package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery2.parameter.resp.GetSellerOrderCountResp;
import com.ymatou.iapi.trading.sellerquery2.parameter.sp.Sp_GetSellerOrderCount;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderCountArguments;
import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderCountBean;
import com.ymatou.query.seller.trading.api.parameter.SellerQuery.GetSellerOrderCountResult;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.common.entitie.SellerOrderStatusParams;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 获取商家订单数量 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_GetSellerOrderCount {

	private static GetSellerOrderCountBean request;
	private static SellerOrderStatusParams sellerOrderStatusParams;
	//	private static com.ymatou.query.seller.trading.api.service.GetSellerOrderCountCall call;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetSellerOrderCountCall call;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商家订单数量");
	}

	@Before
	public void caseUp() {
		request = new GetSellerOrderCountBean();
		sellerOrderStatusParams = new SellerOrderStatusParams();
		//		call = new com.ymatou.query.seller.trading.api.service.GetSellerOrderCountCall();
		call = new com.ymatou.iapi.trading.sellerquery2.service.GetSellerOrderCountCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verify() {
		GetSellerOrderCountResp expectedResponse = new GetSellerOrderCountResp();
		ConstructExpectedResponse(expectedResponse);
		GetSellerOrderCountResp actualResponse = call
				.getSellerOrderCountResponse();
		Map<String, Integer> expect = expectedResponse.getAllRecordCountDic();
		Map<String, Integer> actual = actualResponse.getAllRecordCountDic();
		if (expect == null || expect.size() == 0) {
			DataUtil.verify(true, actual == null || actual.size() == 0,
					"验证AllRecordCountDic == null 或者 AllRecordCountDic.size()==0");
		} else {
			DataUtil.verify(expect.size(), actual.size(),
					"验证AllRecordCountDic.size()");
		}
		Iterator<String> keys = expect.keySet().iterator();
		while (keys.hasNext()) {
			String key = keys.next();
			boolean exists = actual.containsKey(key);
			DataUtil.verify(true, exists, "判断期望的key值:" + key + "是否存在");
			if (exists) {
				DataUtil.verify(expect.get(key), actual.get(key), "判断期望的Key:"
						+ key + "的值是否正确");
			}
		}
	}

	private void ConstructExpectedResponse(GetSellerOrderCountResp response) {
		GetSellerOrderCountArguments args = new GetSellerOrderCountArguments();
		args.setBeginTime(request.getBeginTime());
		args.setEndTime(request.getEndTime());
		args.setIsShangouOrder(request.getIsShangouOrder());
		args.setSellerId(request.getSellerId());
		if (request.getOrderStatusList() != null
				&& request.getOrderStatusList().contains(
						OrderStatusEnum.OrderEstablish.getValue())
				&& request.getOrderStatusList().contains(
						OrderStatusEnum.AccountPaid.getValue())
				&& request.getOrderStatusList().contains(
						OrderStatusEnum.Shipped.getValue())
				&& request.getOrderStatusList().contains(
						OrderStatusEnum.Received.getValue())
				&& request.getOrderStatusList().contains(
						OrderStatusEnum.SellerAccept.getValue())
				&& request.getOrderStatusList().contains(
						OrderStatusEnum.UserAcceptCancel.getValue())
				&& request.getOrderStatusList().contains(
						OrderStatusEnum.SellerAcceptCancel.getValue())
				&& request.getOrderStatusList().contains(
						OrderStatusEnum.SystemCancel.getValue())) {
			List<Integer> allStatus = new ArrayList<Integer>();
			allStatus.add(OrderStatusEnum.OrderEstablish.getValue());
			allStatus.add(OrderStatusEnum.AccountPaid.getValue());
			allStatus.add(OrderStatusEnum.Shipped.getValue());
			allStatus.add(OrderStatusEnum.Received.getValue());
			allStatus.add(OrderStatusEnum.SellerAccept.getValue());
			allStatus.add(OrderStatusEnum.UserAcceptCancel.getValue());
			allStatus.add(OrderStatusEnum.SellerAcceptCancel.getValue());
			allStatus.add(OrderStatusEnum.SystemCancel.getValue());
			allStatus.add(OrderStatusEnum.ApplyForPostPay.getValue());
			args.setOrderStatusList(allStatus);
		} else {
			args.setOrderStatusList(request.getOrderStatusList());
		}
		Map<String, Integer> dict = new HashMap<String, Integer>();
		List<GetSellerOrderCountResult> result = Execute(args);
		if (DataUtil.isNullOrEmpty(result) == false) {
			int total = 0;
			int n = 0;
			for (GetSellerOrderCountResult rs : result) {
				if (rs.getiTradingStatus() == OrderStatusEnum.SellerAccept
						.getValue()) {
					dict.put(
							rs.isbPaidInFull() ? String
									.valueOf(OrderStatusEnum.SellerAccept
											.getValue()) : String
									.valueOf(OrderStatusEnum.OrderPostPay
											.getValue()), DataUtil.Integerr
									.GetValueOrDefault(rs.getOrderCount()));
				} else {
					if (dict.containsKey(String.valueOf(rs.getiTradingStatus()))) {
						n = 0;
					}
					dict.put(String.valueOf(rs.getiTradingStatus()),
							n + rs.getOrderCount());
				}
				total += rs.getOrderCount();
			}
			dict.put(String.valueOf(OrderStatusEnum.Unkown.getValue()), total);// 所有
			if (!DataUtil.isNullOrEmpty(request.getOrderStatusList())) {
				for (int status : request.getOrderStatusList()) {
					if (!dict.containsKey(String.valueOf(status))) {
						dict.put(String.valueOf(status), 0);
					}
				}
			}
		}
		response.setAllRecordCountDic(dict);
	}

	@SuppressWarnings("rawtypes")
	private List<GetSellerOrderCountResult> Execute(
			GetSellerOrderCountArguments args) {
		Sp_GetSellerOrderCount sp = new Sp_GetSellerOrderCount();
		sp.setSellerId(args.getSellerId());
		sp.setIsShangouOrder(args.getIsShangouOrder());
		sp.setBeginTime(args.getBeginTime());
		sp.setEndTime(args.getEndTime());
		sp.setOrderStatus(args.getOrderStatusList());
		List<Map> result = sp.execute();
		return ConvertResult(result);
	}

	@SuppressWarnings("rawtypes")
	private List<GetSellerOrderCountResult> ConvertResult(List<Map> result) {
		List<GetSellerOrderCountResult> l = new ArrayList<GetSellerOrderCountResult>();
		if (!DataUtil.isNullOrEmpty(result)) {
			for (Map m : result) {
				GetSellerOrderCountResult o = new GetSellerOrderCountResult();
				//				o.setbPaidInFull(DataUtil.Booleann.GetValueOrDefault(m
				//						.get("bPaidInFull")));
				//				o.setiTradingStatus(DataUtil.Integerr.GetValueOrDefault(m
				//						.get("iTradingStatus")));
				//				o.setOrderCount(DataUtil.Integerr.GetValueOrDefault(m
				//						.get("OrderCount")));
				DataUtil.mapToBean(o, m);
				l.add(o);
			}
		}
		return l;
	}

	// 查询忽略风控 2016-08-21 http://jira.ymatou.cn:8080/browse/JIRACONN-823
	@SuppressWarnings("unused")
	@Deprecated
	private List<Integer> setSellerOrderStatusControlParams() {
		List<Integer> result = null;
		List<Integer> OrderStatusList = request.getOrderStatusList();
		if (null != OrderStatusList) {
			if (SellerOrderStatusParams.RiskControllerDisabled) {
				sellerOrderStatusParams.setConsiderOrderStatus(true);
				sellerOrderStatusParams.setConsiderRestOrderStatus(true);
				return OrderStatusList;
			}
			sellerOrderStatusParams.setConsiderRiskVerfiedStatus(true);
			List<Integer> OrderStatusList2 = OrderStatusList;
			sellerOrderStatusParams.setConsiderOrderStatus(true);

			if (OrderStatusList2.contains(OrderStatusEnum.OrderEstablish
					.getValue())) {
				sellerOrderStatusParams.setConsiderRCOrderEstablish(true);
				OrderStatusList2
						.removeIf(a -> a == OrderStatusEnum.OrderEstablish
								.getValue());
			}
			if (OrderStatusList2.contains(OrderStatusEnum.AccountPaid
					.getValue())) {
				sellerOrderStatusParams.setConsiderRCAccountPaid(true);
				OrderStatusList2.removeIf(a -> a == OrderStatusEnum.AccountPaid
						.getValue());
			}
			if (OrderStatusList2.size() > 0) {
				sellerOrderStatusParams.setConsiderRestOrderStatus(true);
			}
			result = OrderStatusList2;
		}
		return result;
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_001() {
		Logger.start(true, "获取商家非闪购订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-30,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-3,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			// 是否为闪购订单
			//			request.setIsShangouOrder(false);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(4);
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			// 发送请求
			call.setData(request);
			call.callService();
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_002() {
		Logger.start(true, "获取商家下单状态的订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.OrderEstablish.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_003() {
		Logger.start(true, "获取商家已支付状态的订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.AccountPaid.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_004() {
		Logger.start(true, "获取商家已发货状态的订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.Shipped.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_005() {
		Logger.start(true, "获取商家已收货状态的订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.Received.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_006() {
		Logger.start(true, "获取买家取消状态的订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.UserAcceptCancel.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_007() {
		Logger.start(true, "获取买手取消状态的订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.SellerAcceptCancel.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_008() {
		Logger.start(true, "获取系统取消状态的订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.SystemCancel.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_009() {
		Logger.start(true, "获取买手接单状态的订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-40,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(-5,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.SellerAccept.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_010() {
		Logger.start(true, "获取买手订单数量成功,状态全传1,2,3,4,17,12,13,18");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-2,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(OrderStatusEnum.OrderEstablish.getValue());
			OrderStatusList.add(OrderStatusEnum.AccountPaid.getValue());
			OrderStatusList.add(OrderStatusEnum.Shipped.getValue());
			OrderStatusList.add(OrderStatusEnum.Received.getValue());
			OrderStatusList.add(OrderStatusEnum.SellerAccept.getValue());
			OrderStatusList.add(OrderStatusEnum.UserAcceptCancel.getValue());
			OrderStatusList.add(OrderStatusEnum.SellerAcceptCancel.getValue());
			OrderStatusList.add(OrderStatusEnum.SystemCancel.getValue());
			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderCount_011() {
		Logger.start(true, "获取买手订单数量成功");
		try {
			// 准备参数
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 开始时间
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-1,
					YMTDateUtil.YMDTHMS_STRING);
			request.setBeginTime(BeginTime);
			// 结束时间
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1,
					YMTDateUtil.YMDTHMS_STRING);
			request.setEndTime(EndTime);
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();

			OrderStatusList.add(OrderStatusEnum.AccountPaid.getValue());

			request.setOrderStatusList(OrderStatusList);
			request.setSellerId(SellerId);
			//			request.setIsShangouOrder(false);
			// 发送请求
			call.setData(request);
			call.callService();
			request.setIsShangouOrder(false);
			Logger.verifyEquals(ECode.SUCCESS, call.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}